import {
  Component
} from '@angular/core';
import {
  NavController,
  NavParams,
  ToastController
} from 'ionic-angular';
import {
  Register2Page
} from '../register2/register2';
import {
  LoginPage
} from '../login/login';
import {
  baseCommon
} from '../../common/baseCommon';
import {
  Http,
  Headers,
  RequestOptions,
  URLSearchParams
} from '@angular/http';
import { AppConfig } from'./../../app/app.config';
import { FindPage } from '../find/find';

/**
 * Generated class for the RegisterPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */


@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
})
export class RegisterPage{

  phone: any;

  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public http: Http,
    public toastCtrl: ToastController) {
  }

  ionViewDidLoad() {
    // console.log('ionViewDidLoad RegisterPage');
  }

  RegisterMessage() {
    this.getRegisterMessage(AppConfig.httpLoc+'/register/message',this.phone);
  }

  back() {
    this.navCtrl.push(LoginPage);
  }

  find(){
    this.navCtrl.push(FindPage);
  }


  /**
   * 输入手机号获得验证码
   *
   * @param {any} apiUrl
   * @param {any} phone
   * @memberof RegisterPage
   */
  getRegisterMessage(apiUrl,phone){
    // var loading = super.showLoading(this.loadingCtrl, "登录中...");
    let headers = new Headers({
      'Content-Type': 'application/x-www-form-urlencoded'
    });

    let options = new RequestOptions({
      headers: headers
    });

    let pramas :URLSearchParams = new URLSearchParams();

    pramas.set('phone',phone);

    let getData:any;

    this.http.post(apiUrl, pramas, options)
      .toPromise()
      .then( data => {
        getData = data.json();
        if(getData.message == "成功"){
          this.navCtrl.push(Register2Page,{'phone':this.phone})
        }else{
          baseCommon.showToast(this.toastCtrl,getData.message);
          this.navCtrl.push(LoginPage);
        }
      }, err => {
        console.log( err );
      })
    }
}
